﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Painter
{
    public class LayerState
    {
        public int layer_index; //图层列表的顺序
        public bool bVisible=true;
        public string layer_name;
    }
    /// <summary>
    /// LayerWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LayerControl : UserControl
    {
        public string[] mStateData { get { return new string[] { "正常","叠加","强光","柔光" }; } }
        public string[] mModelData { get { return new string[] { "100%", "90%", "80%", "70%", "60%", "50%", "40%", "30%", "20%", "10%", "0%" }; } }

        LayerCanvas _canvas;
        public LayerCanvas mCanvas
        {
            get { return _canvas; }
            set
            {
                _canvas = value;
                layerList.ItemsSource = _canvas?.Layers;
                layerList.SelectedIndex = _canvas?.mActiveLayer??-1;
                if (mLayerStatusChanged == null && _canvas!=null)
                    mLayerStatusChanged += _canvas.LayerPanel_mLayerStatusChanged;
                if (mLayerSelectChanged == null && _canvas!=null)
                    mLayerSelectChanged += _canvas.LayerPanel_mLayerSelectChanged;
            }
        }
        public delegate void LayerStatusChange(LayerState state);
        public event LayerStatusChange mLayerStatusChanged;
        public delegate void LayerSelectChange(int nIndex);
        public event LayerSelectChange mLayerSelectChanged;

        public LayerControl()
        {
            InitializeComponent();
            layerCombo.DataContext = this;
            transCombo.DataContext = this;
            fillCombo.DataContext = this;
        }
        private void LayerList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mLayerSelectChanged?.Invoke(layerList.SelectedIndex);
        }
        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            int index = layerList.Items.IndexOf((sender as Image).DataContext);
            LayerState item = mCanvas.mOrderBackup.ElementAt(index);
            if (((sender as Image).Tag as string).Contains("闭"))
            {
                item.bVisible = true;
                (sender as Image).Tag = "眼睛";
                mLayerStatusChanged?.Invoke(item);
                (sender as Image).Source = new BitmapImage(new Uri("pack://application:,,,/Resource/view.png"));
            }
            else
            {
                item.bVisible = false;
                (sender as Image).Tag = "闭眼睛";
                mLayerStatusChanged?.Invoke(item);
                (sender as Image).Source = new BitmapImage(new Uri("pack://application:,,,/Resource/close_eye.png"));
            }
            e.Handled = true;
        }
        private void TransCombo_Selected(object sender, SelectionChangedEventArgs e)
        {
            if (_canvas != null)
            {
                ComboBox combo = sender as ComboBox;
                SolidColorBrush brush = _canvas.ActiveCanvas.Background as SolidColorBrush;
                double percent = (combo.Items.Count - combo.SelectedIndex-1)% combo.Items.Count / 10.0;
                _canvas.ActiveCanvas.Opacity = percent;
            }
        }
        private void LayerCombo_Selected(object sender, SelectionChangedEventArgs e)
        {

        }
        private void FillCombo_Selected(object sender, SelectionChangedEventArgs e)
        {

        }
        private void NewLayer_Button_Click(object sender, RoutedEventArgs e)
        {
            string layer_name="";
            for (int i = 1; i < 100; i++)
            {
                layer_name = "新建图层" + i;
                if (!_canvas.Layers.Contains(layer_name))
                    break;
            }

            try{
                _canvas?.AddLayer(layer_name);
                layerList.ItemsSource = _canvas?.Layers;
            }
            catch { }
        }
        private void DelLayer_Button_Click(object sender, RoutedEventArgs e)
        {
            try{
                int index = layerList.SelectedIndex;
                _canvas?.DelLayer(_canvas.mActiveLayer);

                if (index >= _canvas.mActiveLayer)
                    layerList.SelectedIndex = index - 1;
                _canvas.mActiveLayer = layerList.SelectedIndex;
                layerList.ItemsSource = _canvas?.Layers;
            }
            catch { }
        }
    }
}
